package com.usian.controller;

import com.usian.Result;
import com.usian.feign.SSOFeign;
import com.usian.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RequestMapping("/frontend/sso")
@RestController
public class SSOController {

    @Autowired
    private SSOFeign ssoFeign;

    /**
     * 校验用户注册信息
     *
     * @param checkValue
     * @param checkFlag
     * @return
     */
    @RequestMapping("/checkUserInfo/{checkValue}/{checkFlag}")
    public Result checkUserInfo(@PathVariable String checkValue,
                                @PathVariable int checkFlag){
        boolean result = ssoFeign.checkUserInfo(checkValue,checkFlag);
//        true  有  false  没有
        if (!result) {
            return Result.ok(true);
        }
        return Result.error("false");
    }

    /**
     * 用户注册
     *
     * @param tbUser
     * @return
     */
    @RequestMapping("userRegister")
    public Result userRegister(TbUser tbUser){
        boolean result = ssoFeign.userRegister(tbUser);
//        true 注册成功  false  注册失败
        if (result){
            return Result.ok("注册成功");
        }
        return Result.error("注册失败");
    }

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/userLogin")
    public  Result userLogin(@RequestParam String username,
                             @RequestParam String password){
        HashMap hashMap = ssoFeign.userLogin(username,password);
        if (hashMap!=null){
            return Result.ok(hashMap);
        }
        return Result.error("登录失败");
    }

    /**
     * 更新redis中token的有效期
     *
     * @param token
     * @return
     */
    @RequestMapping("/getUserByToken/{token}")
    public Result getUserByToken(@PathVariable String token){
        boolean result = ssoFeign.getUserByToken(token);
        if (result){
            return Result.ok();
        }
        return Result.error("更新失败");
    }

    /**
     * 退出登录
     *
     * @param token
     * @return
     */
    @RequestMapping("/logOut")
    public Result logOut(String token){
        boolean result = ssoFeign.logOut(token);
        if (result){
            return Result.ok();
        }
        return Result.error("退出失败");
    }
}
